
package com.plian.system.controller.im;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.DateUtil;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.entity.im.FundProjectExecution;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.im.IFundProjectExecutionService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.im.FundProjectExecutionVO;
import com.plian.system.vo.im.FundProjectVO;
import com.plian.system.wrapper.im.FundProjectExecutionWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * 基金项目投资计划执行
 控制器
 *
 * @author BladeX
 * @since 2022-04-15
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/fundprojectexecution")
@Api(value = "FundProjectExecutionController", tags = "基金项目投资计划执行 控制器")
public class FundProjectExecutionController extends BaseController {

	private final IFundProjectExecutionService fundProjectExecutionService;

	private FormWorkflowService formWorkflowService;

	private FundProjectExecutionWrapper fundProjectExecutionWrapper;


	/**
	 * 详情
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入fundProjectExecution")
	public R<FundProjectExecutionVO> detail(@RequestBody FundProjectExecution fundProjectExecution) {
		FundProjectExecution detail = fundProjectExecutionService.getOne(Condition.getQueryWrapper(fundProjectExecution));
		FundProjectExecutionVO fundProjectExecutionVO=fundProjectExecutionWrapper.entityToVO(detail);
		return fundProjectExecutionService.getDetailObj(fundProjectExecutionVO);
	}

//	@PostMapping("/updateProject") TODO DEL
//	@ApiOperation(value = "更新基金信息", notes = "传入 Id")
//	public R setNewFundProjectInfo(@RequestBody FundProjectExecution fundProjectExecution) {
//		 fundProjectExecutionService.updateProject(fundProjectExecution);
//		return R.success();
//	}

	/**
	 * 分页 基金项目投资计划执行

	 */
	@PostMapping("/list")
	@ApiOperation(value = "分页", notes = "传入fundProjectExecution")
	public ListResult<FundProjectExecutionVO> list(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		if (query.getAscs()==null&&query.getDescs()==null){
			query.setDescs("filing_month");
		}
		QueryWrapper<FundProjectExecution> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new FundProjectExecution()), entityList
		);
		TokenSearch<FundProjectExecution> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);
		IPage<FundProjectExecution> pages = fundProjectExecutionService.page(Condition.getPage(query), queryWrapper);
		List<FundProjectExecutionVO> fundProjectExecutionVOS = fundProjectExecutionWrapper.entityToVO(pages.getRecords());
		ListResult<FundProjectExecutionVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(fundProjectExecutionVOS);
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}

	/**
	 * 自定义分页 基金项目投资计划执行

	 */
	@PostMapping("/page")
	@ApiOperation(value = "分页", notes = "传入fundProjectExecution")
	public R<IPage<FundProjectExecutionVO>> page(FundProjectExecutionVO fundProjectExecution, Query query) {
		IPage<FundProjectExecutionVO> pages = fundProjectExecutionService.selectFundProjectExecutionPage(Condition.getPage(query), fundProjectExecution);
		return R.data(pages);
	}

	/**
	 * 新增 基金项目投资计划执行

	 */
	@PostMapping("/save")
	@LogOperation("基金项目投资计划执行-保存")
	@ApiOperation(value = "新增", notes = "传入fundProjectExecution")
	public R save(@Valid @RequestBody FundProjectExecution fundProjectExecution) {
		return R.status(fundProjectExecutionService.save(fundProjectExecution));
	}

	/**
	 * 修改 基金项目投资计划执行

	 */
	@PostMapping("/update")
	@LogOperation("基金项目投资计划执行-编辑")
	@ApiOperation(value = "修改", notes = "传入fundProjectExecution")
	public R update(@Valid @RequestBody FundProjectExecution fundProjectExecution) {
		return R.status(fundProjectExecutionService.updateById(fundProjectExecution));
	}

	/**
	 * 新增或修改 基金项目投资计划执行

	 */
	@PostMapping("/submit")
	@LogOperation("基金项目投资计划执行-提交")
	@ApiOperation(value = "新增或修改", notes = "传入fundProjectExecution")
	public R submit(@Valid @RequestBody FundProjectExecution fundProjectExecution) {
		String checkMessage=fundProjectExecutionService.checkMonth(fundProjectExecution);
		if (StringUtil.isNotBlank(checkMessage)){
			return new R(CustomCode.builder().message(checkMessage).code(1999).success(false).build());
		}
		Boolean result=fundProjectExecutionService.submit(fundProjectExecution);
		HashMap<String, Object> processVariable = new HashMap<>();
		formWorkflowService.startProcessAndSimulate(String.valueOf(fundProjectExecution.getId()),fundProjectExecution.getFormType(),processVariable);
		return result ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}

	
	/**
	 * 删除 基金项目投资计划执行

	 */
	@PostMapping("/remove")
	@LogOperation("基金项目投资计划执行-删除")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return fundProjectExecutionService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
				? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}

	/**
	 * 上月的基金项目情况
	 * @param fundProjectExecution
	 * @return
	 */
	@PostMapping("/getLastMonthFundProjectList")
	@ApiOperation(value = "上月的基金项目情况")
	public ListResult<FundProjectVO> getLastMonthFundProjectList(@RequestBody FundProjectExecution fundProjectExecution){
		ListResult<FundProjectVO> listResult=new ListResult<>(CommonCode.SUCCESS);
		String filingMonth=fundProjectExecution.getFilingMonth();
		String lastMonth=DateUtil.getLastMonth(filingMonth);
		List<FundProjectVO> list=fundProjectExecutionService.getFundProjectListByOrgAndMonth(lastMonth,fundProjectExecution.getFilingUnitId());
		listResult.setTotal(list.size());
		listResult.setList(list);
		return listResult;
	}

	@GetMapping("/refreshDetailData")
	@LogOperation("基金项目投资计划执行-刷新数据")
	public void refreshDetailData(){
		fundProjectExecutionService.refreshDetailData();
	}
}
